class_name LaserShooterForGun
extends Node2D

@onready var laser_beam: LaserBeam = $LaserBeam
@onready var animation_player: AnimationPlayer = $AnimationPlayer

var sender: Node2D = null
var attack: float = 30
var critical_rate: float = 0.5
var critical_bonus: float = 0.5
var knockback: float = 250
var hit_cam_shake_strength: float = 0.7
var hit_cam_shake_duration: float = 0.1

var target_position: Vector2
var target_dir: Vector2
@export var move_speed: float = 150
@export var rotate_speed: float = 5.0
var is_arrive_target: bool = false

@onready var shoot_audio: AudioStreamPlayer2D = $AudioStreamPlayer2D


func _ready() -> void:
	laser_beam.set_is_enemy(false)
	laser_beam.sender = sender
	laser_beam.attack = attack
	laser_beam.critical_rate = critical_rate
	laser_beam.critical_bonus = critical_bonus
	laser_beam.knockback = knockback
	laser_beam.hit_cam_shake_strength = hit_cam_shake_strength
	laser_beam.hit_cam_shake_duration = hit_cam_shake_duration
	
	animation_player.play("idle")
	
	shoot_audio.play()
	

func _physics_process(delta: float) -> void:
	proces_move_2_target(delta)
	

func proces_move_2_target(delta: float):
	if is_arrive_target: return
	
	global_position = global_position.move_toward(target_position, move_speed * delta)
	global_rotation += rotate_speed * delta
	
	if global_position == target_position:
		is_arrive_target = true
		global_rotation = target_dir.angle()
		shoot()
		
		
func shoot():
	animation_player.play("shoot")
	await animation_player.animation_finished
	animation_player.play("destroy")
	
	
